#include "includes.h"

int main() {
	struct msg_view_change *msg;
	char *ser;
	
	
	init_ds();
	
	msg = create_view_change(1, 10);
	ser = serialize_view_change(msg);
	
	printf(">>%s<<\n", ser);
	
	msg = deserialize_view_change(ser);
	printf("server id : %d, attempted: %d\n\n", msg->server_id, msg->attempted);
	
	
	struct msg_client_update *cu_msg;
	char update[] = "test_update";
	cu_msg = create_client_update(1, 2, 123, update );
	ser = serialize_client_update(cu_msg);
	printf(">>%s<<\n", ser);
	
	cu_msg = deserialize_client_update(ser);
	printf("client id : %d, server id : %d, timestamp : %d, update : %s\n", 
		cu_msg->client_id, cu_msg->server_id, cu_msg->timestamp, cu_msg->update);
	
	
	
	struct msg_prepare *prep_msg;
	
	prep_msg = create_prepare(1, 2, 4);
	ser = serialize_prepare(prep_msg);
	printf(">>%s<<\n", ser);
	prep_msg = deserialize_prepare(ser);
	printf("server id : %d, view : %d, local aru : %d\n", 
		prep_msg->server_id, prep_msg->view, prep_msg->local_aru);
		
	
	
	init_host_list();
	add_host("test1", 1);
	add_host("test2", 2);
	add_host("test3", 3);
	add_host("test4", 4);
	add_host("test5", 5);
	add_host("test6", 6);

	printf("host id of test6 : %d\n", get_host_id("test6"));
	printf("Getting host name of id 4 : %s\n\n", get_host_name(4));	
	
	
	
	
	//Create a gou
	struct msg_globally_ordered_update *gou;
	gou = create_gou(1, 2, "testing");
	ser = serialize_gou(gou);
	printf(">>%s<<\n", ser);
	
	//Create a proposal
	struct msg_proposal *prop;
	prop = create_proposal(1,1, 2, "testing");
	ser = serialize_proposal(prop);
	printf(">>%s<<\n", ser);


	struct msg_prepare_ok *po;
	//Test prepare ok
	update_data_structures(serialize_proposal(create_proposal(1, 1, 1, "update1")));
	update_data_structures(serialize_proposal(create_proposal(1, 1, 2, "update2")));
	update_data_structures(serialize_proposal(create_proposal(1, 1, 3, "update3")));
	
	update_data_structures(serialize_gou(create_gou(1, 1, "update1")));
	update_data_structures(serialize_gou(create_gou(1, 2, "update2")));
	
	ser = serialize_prepare_ok(create_prepare_ok(1,1));

	printf(">>%s<<\n", ser);
	
	po = deserialize_prepare_ok(ser);
	clear_history();
	
	struct msg_view_change* tmp_vc = create_view_change(3, 2);
	update_data_structures(serialize_view_change(create_view_change(1, 2)));
	update_data_structures(serialize_view_change(create_view_change(2, 2)));
	update_data_structures(serialize_prepare_ok(po));
	ser = serialize_prepare_ok(create_prepare_ok(1,1));
	printf(">>%s<<\n", ser);
	clear_history();
	ser = serialize_prepare_ok(create_prepare_ok(1,1));
	printf(">>%s<<\n", ser);
	
	
	
	
	struct global_history *tmp_gh = deserialize_gh("GLOBAL_HISTORY:20:|ACCEPT:14:0:20;ACCEPT:7:0:20|GOU:14:20:Update10");
	
	ser = serialize_gh(tmp_gh);
	printf("%s\n",ser);
	
}
